/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/**
 *
 * @author TiTa
 */
public class lay_hinh_face {
   

    DomElement doc;
     public  void lay_hinh_khong_trang(String url){
         
         try{
         java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
         java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
     //--!end warning
    final WebClient webClient = new WebClient();
     //webClient.getOptions().setTimeout(2000);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getCookieManager().setCookiesEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.waitForBackgroundJavaScript(10000);
  //  url="https://www.facebook.com/pages/V%E1%BB%ABng-%C6%A1i-m%E1%BB%9F-ra-Cafe/141160225904005?id=141160225904005&sk=photos_stream";//ket noi trang
 //   url=them_photos_stream(url);
    final HtmlPage page=webClient.getPage(url);
    
    
    
    //lay dia chi ok
  /*  final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@class='fbTimelineSummarySectionWrapper']").get(0);
       doc=div.getElementsByTagName("span").get(6);
     
    String link=doc.getElementsByTagName("a").get(0).getTextContent();
    System.out.println(link);
    //////////////////////////
    //lay dia chi tot hon
      final HtmlSpan div1 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(1);
       
       doc=div1.getElementsByTagName("span").get(0);
       String link1=doc.getElementsByTagName("a").get(0).getTextContent();
       System.out.println(link1);

        //dien thoai
       final HtmlSpan div2 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(2);
       
       String d=div2.getElementsByTagName("span").get(0).getTextContent();
    
       System.out.println(d);
*/
      //lay hinh
    int so_hinh=page.getByXPath("//div[@class='tagWrapper']").size();
    System.out.println(so_hinh);
    for(int i=0;i<so_hinh;i++){
       final HtmlDivision hinh = (HtmlDivision) page.getByXPath("//div[@class='tagWrapper']").get(i);
       
        String the_i=hinh.getElementsByTagName("i").get(0).getAttribute("style");
         
        String da_cat=cat_theo_ki_tu(the_i);
         System.out.println(da_cat+"\n");
             }   
   
         }
         
         catch(Exception ex){
         
         ex.printStackTrace();
         }
         
     }
      public String cat_theo_ki_tu(String input)
    {
     
        
        int do_dai=input.length();
        String input_cat_dau=input.substring(22, do_dai-2);
   //khong can thiet
     //  String[] arr;
      // arr= input.split("");
      //
            String ss=input_cat_dau;
         int do_dai1=input_cat_dau.length();
       input_cat_dau = input_cat_dau.substring(0,58);
          //System.out.println(input_cat_dau);
            ss = ss.substring(67,do_dai1);
           //  System.out.println(ss);
             input_cat_dau=input_cat_dau+ss;
           //   System.out.println(input_cat_dau);
        
       
        return input_cat_dau;
    }
   

private String them_photos_stream(String url){
      
        String s=url+"/photos_stream";
        return s;
  }
     
     public static void main(String[] args) throws UnsupportedEncodingException{
     
         
        lay_hinh_face lh=new lay_hinh_face();
        String url="https://www.facebook.com/media/set/?set=a.10151253390061951.452237.135833866950&type=1";
        String url1=URLDecoder.decode(url,"UTF-8");
       System.out.println(url1);
             lh.lay_hinh_khong_trang(url);
          //   lh.cat_theo_ki_tu();
             // lh.DownloadPage("https://www.facebook.com/hardrockcafevietnam");
         }
     


}
